using System;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Hearthbuddy.Controls
{
	/// <summary>
	/// Implements a "split button" for Silverlight and WPF.
	/// </summary>
	[TemplatePart(Name = "SplitElement", Type = typeof(UIElement))]
	public class SplitButton : Button
	{
		private const string string_0 = "SplitElement";

		private UIElement uielement_0;

		private ContextMenu contextMenu_0;

		private DependencyObject dependencyObject_0;

		private Point point_0;

		private ObservableCollection<object> observableCollection_0 = new ObservableCollection<object>();

		[CompilerGenerated]
		private bool bool_0;

		/// <summary>
		/// Gets the collection of items for the split button's menu.
		/// </summary>
		public Collection<object> ButtonMenuItemsSource => observableCollection_0;

		/// <summary>
		/// Gets or sets a value indicating whetherthe mouse is over the split element.
		/// </summary>
		protected bool IsMouseOverSplitElement
		{
			[CompilerGenerated]
			get
			{
				return bool_0;
			}
			[CompilerGenerated]
			private set
			{
				bool_0 = value;
			}
		}

		/// <summary>
		/// Initializes a new instance of the SplitButton class.
		/// </summary>
		public SplitButton()
		{
			base.DefaultStyleKey = typeof(SplitButton);
		}

		/// <summary>
		/// Called when the template is changed.
		/// </summary>
		public override void OnApplyTemplate()
		{
			if (uielement_0 != null)
			{
				uielement_0.MouseEnter -= uielement_0_MouseEnter;
				uielement_0.MouseLeave -= uielement_0_MouseLeave;
				uielement_0 = null;
			}
			if (contextMenu_0 != null)
			{
				contextMenu_0.Opened -= contextMenu_0_Opened;
				contextMenu_0.Closed -= contextMenu_0_Closed;
				contextMenu_0 = null;
			}
			if (dependencyObject_0 != null)
			{
				RemoveLogicalChild(dependencyObject_0);
				dependencyObject_0 = null;
			}
			base.OnApplyTemplate();
			uielement_0 = GetTemplateChild("SplitElement") as UIElement;
			if (uielement_0 == null)
			{
				return;
			}
			uielement_0.MouseEnter += uielement_0_MouseEnter;
			uielement_0.MouseLeave += uielement_0_MouseLeave;
			contextMenu_0 = ContextMenuService.GetContextMenu(uielement_0);
			if (contextMenu_0 != null)
			{
				contextMenu_0.IsOpen = true;
				DependencyObject parent = contextMenu_0;
				do
				{
					dependencyObject_0 = parent;
					parent = LogicalTreeHelper.GetParent(parent);
				}
				while (parent != null);
				contextMenu_0.IsOpen = false;
				AddLogicalChild(dependencyObject_0);
				contextMenu_0.Opened += contextMenu_0_Opened;
				contextMenu_0.Closed += contextMenu_0_Closed;
			}
		}

		/// <summary>
		/// Called when the Button is clicked.
		/// </summary>
		protected override void OnClick()
		{
			if (IsMouseOverSplitElement)
			{
				OpenButtonMenu();
			}
			else
			{
				base.OnClick();
			}
		}

		/// <summary>
		/// Called when a key is pressed.
		/// </summary>
		protected override void OnKeyDown(KeyEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			if (Key.Down != e.Key && Key.Up != e.Key)
			{
				base.OnKeyDown(e);
			}
			else
			{
				base.Dispatcher.BeginInvoke(new Action(OpenButtonMenu));
			}
		}

		/// <summary>
		/// Opens the button menu.
		/// </summary>
		protected void OpenButtonMenu()
		{
			if (0 < observableCollection_0.Count && contextMenu_0 != null)
			{
				contextMenu_0.HorizontalOffset = 0.0;
				contextMenu_0.VerticalOffset = 0.0;
				contextMenu_0.IsOpen = true;
			}
		}

		private void uielement_0_MouseEnter(object sender, MouseEventArgs e)
		{
			IsMouseOverSplitElement = true;
		}

		private void uielement_0_MouseLeave(object sender, MouseEventArgs e)
		{
			IsMouseOverSplitElement = false;
		}

		private void contextMenu_0_Opened(object sender, RoutedEventArgs e)
		{
			point_0 = TranslatePoint(new Point(0.0, base.ActualHeight), contextMenu_0);
			method_0();
			base.LayoutUpdated += SplitButton_LayoutUpdated;
		}

		private void contextMenu_0_Closed(object sender, RoutedEventArgs e)
		{
			base.LayoutUpdated -= SplitButton_LayoutUpdated;
			Focus();
		}

		/// <summary>
		/// Called when the ContextMenu is open and layout is updated.
		/// </summary>
		/// <param name="sender">Event source.</param>
		/// <param name="e">Event arguments.</param>
		private void SplitButton_LayoutUpdated(object sender, EventArgs e)
		{
			method_0();
		}

		private void method_0()
		{
			Point point = default(Point);
			Point point2 = point_0;
			contextMenu_0.HorizontalOffset = point2.X - point.X;
			contextMenu_0.VerticalOffset = point2.Y - point.Y;
			if (FlowDirection.RightToLeft == base.FlowDirection)
			{
				contextMenu_0.HorizontalOffset *= -1.0;
			}
		}
	}
}
